#include <iostream>
#include <unistd.h>
#include <poll.h>

int main()
{
    struct pollfd readfd;
    readfd.fd = 0;
    readfd.events = POLLIN;
    //这里设置0的原因是因为方便系统设置响应的事件宏
    //这些宏都是有属于自己的二进制位为1,其他的二进制位都是0
    readfd.revents = 0;

    while(true)
    {
        int n = poll(&readfd, 1, -1);
        switch(n)
        {
            case -1:
                std::cerr << "poll error!" << std::endl;
                break;
            case 0:
                std::cout << "time out!" << std::endl;
                break;
            default:
                if (readfd.revents & POLLIN)
                {
                    std::cout << readfd.fd << ":上面读事件就绪了" << std::endl;
                    char buffer[1024];
                    ssize_t ret = read(readfd.fd, buffer, sizeof(buffer) - 1);
                    if (ret > 0)
                    {
                        std::cout << "有人说:" << buffer << std::endl;
                    }
                }
                break;
        }
    }

    return 0;
}
